package com.itheima.countdownlatch;

import java.util.concurrent.CountDownLatch;

public class Test {
    public static void main(String[] args) {
//        使用CountDownLatch编写一个程序，实现以下效果：
//	线程A打印：”开始计算”
//	线程B：计算1--100所有数的累加和，并打印结果。
//	线程A打印：”计算完毕”
        CountDownLatch latch = new CountDownLatch(1);
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"开始计算");
                try {
                    latch.await();
                    System.out.println(Thread.currentThread().getName()+"计算完毕");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                int sum=0;
                for (int i = 1; i <=100; i++) {
                    sum +=i;
                }
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(sum);
                latch.countDown();
            }
        }).start();
    }
}
